//
//  CYTicketDetailFooderView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/18.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTicketDetailFooderView.h"
@interface CYTicketDetailFooderView()
@property (nonatomic, strong) CYLabel *titleLBL;//累计中奖
@property (nonatomic, strong) NSArray *data;//金额
@end
@implementation CYTicketDetailFooderView
- (instancetype)initWithColor:(UIColor *)color{
    self = [super initWithColor:color];
    if (self) {
        self.titleLBL.hidden = NO;
    }
    return self;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:@"累计中奖:" size:12 textcolor:ColorHex(0x333333)];
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(AUTO(10));
            make.left.equalTo(self).with.offset(10);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (void)setTotalMoney:(NSString *)totalMoney{
    if (_totalMoney != totalMoney)
        _totalMoney = totalMoney;
    _data = [self getMoneyFormat:totalMoney];
    [self setupSubview];
    DeLog(@"array %@",_data);
    
}

/*获取数字的整亿，整万,整个数*/
- (NSArray *)getMoneyFormat:(NSString *)string{
    
    NSInteger num = [string integerValue];
    NSMutableString *mutableString = [[NSMutableString alloc]init];
    NSInteger billion = num / 100000000;  /*取亿的整倍*/
    NSInteger tenThousand = (num % 100000000)/10000;  /*对亿取余后，取万的整倍*/
    NSInteger one = (num % 10000);    /*对万取余后，取个位的整倍*/
    
    /*判断是否大于亿
     *1，如果大于亿，则以亿为大单位，以万为小单位
     *2，如果小于亿，大于万，则以整万为单位
     *3，如果小于万，则以整个为单位
     */
    if (billion > 0) {
        [mutableString appendString:[NSString stringWithFormat:@"%ld亿", (long)billion]];
    }
    if (tenThousand > 0) {
        [mutableString appendString:[NSString stringWithFormat:@"%ld万", (long)tenThousand]];
    }
    else if (one >0 || one <= 0){
        [mutableString appendString:[NSString stringWithFormat:@"%ld元", (long)one]];
    }
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < mutableString.length; i ++) {
        NSString *str = [mutableString substringWithRange:NSMakeRange(i, 1)];
        [array addObject:str];
    }
    
    return array;
}

- (void)setupSubview{
    
    int num = 0;//汉字的个数
    CGFloat width_number = (_data.count < 10)?30:20;//数字的宽度
    CGFloat width_hanzi = (_data.count < 10)?25:15;//汉字的宽度
    CGFloat font_number = (_data.count < 10)?38:22;
    CGFloat font_hanzi = (_data.count < 10)?16:11;
    CGFloat minus_width = AUTO(5);//号码间隙宽度
    CGFloat total_width = 0;//号码汉字全部宽度
    for (int i = 0; i < _data.count; i ++) {
        BOOL isnumber= [NSString judgeInputIsNumber:_data[i]];
        if (isnumber)
            num++;
    }
    
    
    total_width = width_number * num + width_hanzi *(_data.count - num) + minus_width * (_data.count - 2 * (_data.count - num));
    
    CGFloat offset_x = (SCREEN_WIDTH - total_width) / 2.0;
    
    for (int j = 0; j < _data.count; j ++) {
        
        BOOL isnumber= [NSString judgeInputIsNumber:_data[j]];
        CGFloat font = (isnumber)?font_number:font_hanzi;//字体
        CGFloat width = (isnumber)?width_number:width_hanzi;
        CGFloat minus_width = (isnumber)?AUTO(5):0;
        NSString *image_name = (isnumber)?@"numberGround":@"";//图片名字
        
        CYImageView * img = [[CYImageView alloc]initWithImage:[UIImage imageNamed:image_name]];
        [self addSubview:img];
        
        CYLabel *label =[[CYLabel alloc]initWithText:_data[j] size:font weight:UIFontWeightBlack];
        label.textColor = ColorHex(0xff5b05);
        [img addSubview:label];
        
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).with.offset (offset_x );
            make.top.mas_equalTo(_titleLBL.mas_bottom).with.offset(20);
            make.size.mas_equalTo(CGSizeMake(width, width_number * 7.0 / 5.0));
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(label.bounds.size);
            if (isnumber)
                make.center.equalTo(img);
            else{
                make.bottom.equalTo(img.mas_bottom);
                make.centerX.equalTo(img);
            }
        }];
        
        offset_x = offset_x + width + minus_width;
    }
    
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
